package com.wu.common.handel;

import com.wu.common.exception.WexinReqException;
import com.wu.common.utils.WeiXinConstant;
import com.wu.common.utils.WeiXinReqUtil;
import com.wu.entity.weixin.annotation.ReqType;
import com.wu.entity.weixin.po.WeixinReqConfig;
import com.wu.entity.weixin.requ.DownloadMedia;
import com.wu.entity.weixin.requ.WeixinReqParam;
import net.sf.json.JSONObject;
import org.jdom.JDOMException;

import java.io.IOException;

public class WeiXinReqService {
    private static WeiXinReqService weiXinReqUtil = null;

    private WeiXinReqService() {
        String var1 = WeiXinReqService.class.getClassLoader().getResource("").getFile();

        try {
            WeiXinReqUtil.initReqConfig("weixin-reqcongfig.xml");
        } catch (JDOMException var3) {
            var3.printStackTrace();
        } catch (IOException var4) {
            var4.printStackTrace();
        }

    }

    public static WeiXinReqService getInstance() {
        if (weiXinReqUtil == null) {
            Class var0 = WeiXinReqService.class;
            synchronized(WeiXinReqService.class) {
                if (weiXinReqUtil == null) {
                    weiXinReqUtil = new WeiXinReqService();
                }
            }
        }

        return weiXinReqUtil;
    }

    public String doWeinxinReq(WeixinReqParam weixinReqParam) throws WexinReqException {
        String strReturnInfo = "";
        if (weixinReqParam.getClass().isAnnotationPresent(ReqType.class)) {
            ReqType reqType = (ReqType)weixinReqParam.getClass().getAnnotation(ReqType.class);
            WeixinReqConfig objConfig = WeiXinReqUtil.getWeixinReqConfig(reqType.value());
            WeiXinReqHandler handler = WeiXinReqUtil.getMappingHander(objConfig.getMappingHandler());
            strReturnInfo = handler.doRequest(weixinReqParam);
        }

        return strReturnInfo;
    }

    public JSONObject doWeinxinReqJson(WeixinReqParam weixinReqParam) throws WexinReqException {
        String strResult = this.doWeinxinReq(weixinReqParam);
        JSONObject result = JSONObject.fromObject(strResult);
        Object error = result.get(WeiXinConstant.RETURN_ERROR_INFO_CODE);
        if (error != null && Integer.parseInt(error.toString()) != 0) {
            throw new WexinReqException(result.toString());
        } else {
            return result;
        }
    }

    public static void main(String[] args) {
        String ddd = "";

        try {
            DownloadMedia g = new DownloadMedia();
            g.setFilePath("H:/temp");
            g.setMedia_id("nH-tzebPcZY41Hlao3mjPHpUHHJSIbfP8hbGJy73LUj5BfvVDV9b84uIpZ8Yjlzw");
            g.setAccess_token("bbkXUUyC6F85R_vh7FOokDZn54P4jY6RVg8rvtzd0D10nIgd7Ksg7bBc8mncX6SZ1mMEI7v1q1OBtWoWG8--iR6ohe3kXbx5jUTHGAjGPAU");
            ddd = getInstance().doWeinxinReq(g);
            System.out.println(ddd);
        } catch (WexinReqException var3) {
            var3.printStackTrace();
        }

    }
}
